package com.shujia.flink.sql

import org.apache.flink.streaming.api.scala._
import org.apache.flink.table.api._
import org.apache.flink.table.api.bridge.scala._

object Demo6Onhbase {
  def main(args: Array[String]): Unit = {

    //flink  流处理的环境
    val bsEnv: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment

    //设置flink  sql 配置
    val bsSettings: EnvironmentSettings = EnvironmentSettings
      .newInstance()
      .useBlinkPlanner() // 使用blink 的计划器（解析sql）
      .inStreamingMode()
      .build()

    //创建table 环境
    val bsTableEnv: StreamTableEnvironment = StreamTableEnvironment.create(bsEnv, bsSettings)

    /**
      * 创建 hbase source 表---  有界流
      *
      */

    bsTableEnv.executeSql(
      """
        |CREATE TABLE hbase_student (
        | id STRING,
        | info ROW<name STRING,age STRING>,
        | PRIMARY KEY (id) NOT ENFORCED
        |) WITH (
        | 'connector' = 'hbase-1.4',
        | 'table-name' = 'student',
        | 'zookeeper.quorum' = 'master:2181'
        |)
        |
      """.stripMargin)


    bsTableEnv.executeSql(
      """
        |CREATE TABLE print_table WITH ('connector' = 'print')
        |LIKE hbase_student (EXCLUDING ALL)
        |
        """.stripMargin)


    /*  bsTableEnv.executeSql(
        """
          |insert into print_table
          |select * from hbase_student
          |
        """.stripMargin)*/




    bsTableEnv.executeSql(
      """
        |
        |CREATE TABLE student_file (
        |  id STRING,
        |  name STRING,
        |  age INT,
        |  gender STRING,
        |  clazz STRING
        |)  WITH (
        |  'connector' = 'filesystem',
        |  'path' = 'data/students.txt',
        |  'format' = 'csv',
        |  'csv.field-delimiter'=','
        |)
        |
      """.stripMargin)

    bsTableEnv.executeSql(
      """
        |CREATE TABLE hbase_student_w (
        | id STRING,
        | info ROW<name STRING,age INT,gender STRING, clazz STRING>,
        | PRIMARY KEY (id) NOT ENFORCED
        |) WITH (
        | 'connector' = 'hbase-1.4',
        | 'table-name' = 'student',
        | 'zookeeper.quorum' = 'master:2181'
        |)
        |
      """.stripMargin)


    /**
      * 将数据保存到hbase中
      *
      */

    bsTableEnv.executeSql(
      """
        |insert into hbase_student_w
        |select id,ROW(name,age,gender,clazz) as info from student_file
        |
      """.stripMargin)
  }
}
